//
//  HMEmoticonTools.swift
//  WeiBo14
//
//  Created by HM on 16/3/12.
//  Copyright © 2016年 HM. All rights reserved.
//  表情数据操作的工具类

import UIKit

// 一页的表情数量
private let HMEmoticonsPageCount = 20

class HMEmoticonTools: NSObject {

    // 单例
    static let sharedTools: HMEmoticonTools = HMEmoticonTools()
    
    /// 表情数据的bundle
    lazy var emoticonsBundle: NSBundle = {
        // 1. 获取到 Emoticons.Bundle的路径
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        // 2. 加载bundle
        let bundle = NSBundle(path: path)!
        return bundle
    }()
    
    /// 最近表情
    lazy var recentEmoticons: [HMEmoticon] = {
        return [HMEmoticon]()
    }()
    
    /// 喵小妮
    lazy var niniEmoticons: [HMEmoticon] = {
        return [HMEmoticon]()
    }()
    
    
    /// 读取默认表情
    lazy var defaultEmoticons: [HMEmoticon] = {
        return self.emoticonsWithFolderName("default")
    }()
    
    // Emoji表情
    lazy var emojiEmoticons: [HMEmoticon] = {
        return self.emoticonsWithFolderName("emoji")
    }()
    
    /// 浪小花表情
    lazy var lxhEmoticons: [HMEmoticon] = {
        return self.emoticonsWithFolderName("lxh")
    }()
    
    
    /// collectionView 所需要的表情数据
    lazy var allEmoticons: [[[HMEmoticon]]] = {
        return [
        [self.recentEmoticons],// 因为最近表情只有一页,所以不用去截取
        self.typeEmoticonPages(self.defaultEmoticons),
        self.typeEmoticonPages(self.emojiEmoticons),
        self.typeEmoticonPages(self.lxhEmoticons),
        [self.niniEmoticons]
        ]
    }()
    
    private override init(){}
    
    
    /// 返回某种表情分成的二维数组
    private func typeEmoticonPages(emoticons: [HMEmoticon]) -> [[HMEmoticon]] {
        // 1. 计算出一共有几页
        let pageCount = (emoticons.count - 1) / HMEmoticonsPageCount + 1
        // 定义返回数组
        var result: [[HMEmoticon]] = [[HMEmoticon]]()
        // 2. 遍历页数,截取对应页数的表情数组
        for i in 0..<pageCount {
            
            // 计算从哪儿开始截取
            let location = i * HMEmoticonsPageCount
            // 设置截取的长度
            var len = HMEmoticonsPageCount
            
            if location + len > emoticons.count {
                len = emoticons.count - location
            }
            let range = NSMakeRange(location, len)
            //subarrayWithRange --> 传入一个NSRange,会返回这个范围所对应的数组
            // 每一页的数组
            let subArray = (emoticons as NSArray).subarrayWithRange(range) as! [HMEmoticon]
            // 3. 再把截取出来的数组放到一个数组里面
            result.append(subArray)
        }
        return result
    }
    
    /// 传入表情文件夹的名字,去读取该表情数组
    ///
    /// - parameter folderName: 文件夹名字
    ///
    /// - returns: 表情数组
    private func emoticonsWithFolderName(folderName: String) -> [HMEmoticon] {
        // 1. 从EmoticonBundle里面找到默认表情的info.plist的路径
        let path = self.emoticonsBundle.pathForResource("\(folderName)/info.plist", ofType: nil)!
        
        // 2. 读取 info.plist
        let array = NSArray(contentsOfFile: path)
        // 返回值
        var result = [HMEmoticon]()
        // 3. 遍历字典转模型
        for value in array! {
            let dict = value as! [String: AnyObject]
            // 字典转模型
            let emoticon = HMEmoticon(dict: dict)
            // 记录当前表情按钮所对应的文件夹名字,以便在显示图片表情的时候,读取表情数据
            emoticon.folderName = folderName
            result.append(emoticon)
        }
        // 4. retrun
        return result
    }
}


